const QQMapWX = require('../../qqmap/qqmap-wx-jssdk.js');
const config = require('../../utils/config')
let qqmapsdk;

Page({
  data: {
    latitude: 30.657450224334617,
    longitude: 104.06575420231047,
    markers: [
      {
        id: 1,
        latitude: 30.548265424401453,
        longitude: 104.05980508614721,
        title: '国信安分部',
        iconPath: '/images/market.png',
        width: 16,
        height: 16
      },
      {
        id: 2,
        latitude: 30.565549225082094,
        longitude: 104.05233389867863,
        title: '锦城公园分部',
        iconPath: '/images/market.png',
        width: 16,
        height: 16
      },
      {
        id: 3,
        latitude: 30.54552505269184,
        longitude: 104.05842977393934,
        title: '三街分部',
        iconPath: '/images/market.png',
        width: 16,
        height: 16
      }
    ],
    address: ''
  },
  onLoad: function (options) {
    const that = this
    const windowInfo = wx.getWindowInfo()
    console.log(windowInfo)

    // 获取 map 上下文对象
    this.myMap = wx.createMapContext('mymap')

    // 实例化 qqmap API核心类
    qqmapsdk = new QQMapWX({
      key: config.mapKey
    });

    // 逆地址解析
    qqmapsdk.reverseGeocoder({
      success(e) {
        console.log('默认参数逆地址：', e)
        that.setData({
          address: e.result.address_component.city
        })
      },
      fail(err) {
        console.log('逆地址解析失败：', err)
      }
    })

    this.getMyPosition()
  },
  // 点击地图，获取位置
  getPosition(e) {
    console.log('点击地图：', e)
  },
  // 点击标记点
  getMarker(e) {
    console.log('你选择的标记点是：', e)
  },
  // 获取当前位置
  getMyPosition() {
    const that = this
    wx.getLocation({
      type: 'gcj02',
      success (res) {
        console.log('返回坐标：', res)
        const latitude = res.latitude
        const longitude = res.longitude
        that.setData({
          latitude,
          longitude
        })
      }
     })
  },
  // 回到当前定位点
  moveToMyPosition() {
    this.myMap.moveToLocation()
  },
  searchHandler(e) {
    console.log(e.detail.value)

    qqmapsdk.search({
      keyword: e.detail.value,  //搜索关键词
      location: this.data.latitude + ',' + this.data.longitude,  //设置周边搜索中心点
      success(e) {
        console.log('周边信息：', e)
      },
      fail(err) {
        console.log('周边搜索失败：', err)
      }
    })
  }
})